package sms

import (
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
	"log"
	"poem-server/initialize/config"
)

func SendMessage(info MessageInfo) error {
	client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", config.Config.Sms.AccessKeyID, config.Config.Sms.AccessKeySecret)
	if err != nil {
		log.Println("=======================>sms连接出现错误", err)
		return nil // 不要影响程序主业务 有err也不抛出
	}
	log.Println("=======================>sms连接配置信息", client)

	request := requests.NewCommonRequest()                               // 构造一个公共请求
	request.Method = "POST"                                              // 设置请求方式
	request.Product = "Ecs"                                              // 指定产品
	request.Scheme = "http"                                              // https | http
	request.Domain = "dysmsapi.aliyuncs.com"                             // 指定域名则不会寻址，如认证方式为 Bearer Token 的服务则需要指定
	request.Version = "2017-05-25"                                       // 指定产品版本
	request.ApiName = "SendSms"                                          // 指定接口名
	request.QueryParams["RegionId"] = "cn-hangzhou"                      // 地区
	request.QueryParams["SignName"] = config.Config.Sms.SignName         //阿里云验证过的项目名
	request.QueryParams["TemplateCode"] = config.Config.Sms.TemplateCode //阿里云的短信模板号
	request.QueryParams["PhoneNumbers"] = info.Phone
	var code = info.Message
	request.QueryParams["TemplateParam"] = "{\"code\":" + code + "}" //短信模板中的需要填充的信息内容

	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		log.Println("=======================>发送短信有错误", err)
	}
	log.Println("=======================>信息发送响应", response.String())
	return nil // 不要影响程序主业务 有err也不抛出
}
